﻿using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq.Expressions;
using System;
using System.Text;

namespace Inovout.Web.Html
{
    public static class FormExtensions
    {
       public static MvcHtmlString Property<TModel, TValue>(this HtmlHelper<TModel> html
             , Expression<Func<TModel, TValue>> expression)
       {
           StringBuilder propertyBuilder = new StringBuilder();
           propertyBuilder.Append(html.PropertyCaption(expression).ToHtmlString());
           propertyBuilder.Append(html.PropertyPart(expression).ToHtmlString());
           propertyBuilder.Append(html.ValidationMessageFor(expression).ToHtmlString());

           return MvcHtmlString.Create(propertyBuilder.ToString());
       }
           public static MvcHtmlString PropertyCaption<TModel, TValue>(this HtmlHelper<TModel> html
            , Expression<Func<TModel, TValue>> expression)
        {
            //TypeDescriptor modelTypeDescriptor = TypeDescriptor.Create(typeof(TModel));
            //PropertyDescriptor propertyDescriptor = modelTypeDescriptor.GetProperty(ExpressionHelper.GetExpressionText(expression));
            return html.LabelFor(expression);
        }

           public static MvcHtmlString PropertyPart<TModel, TValue>(this HtmlHelper<TModel> html
            , Expression<Func<TModel, TValue>> expression)
           {
               if (html.ViewBag._ViewMode == ViewMode.ReadOnly)
               {
                   return html.DisplayFor(expression);
               }
               else
               {
                   return html.EditorFor(expression);
               }
           }

    }

}